package net.tycmc.cty.ctyms.timejob.iems.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.tycmc.bulb.common.util.DateUtil;
import net.tycmc.bulb.common.util.MapGetter;
import net.tycmc.cty.ctyms.timejob.function.io.DownLoadNet;
import net.tycmc.cty.ctyms.timejob.iems.dao.WeatherCatchDAO;

import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.codehaus.jackson.map.ObjectMapper;

public class WeatherCatchService {
	
	private static final String CATCHWEATHERURL = "http://m.weather.com.cn/data/";
	private static final String CATCHWEATHERURLSUFFIX = ".html";
	private static final String KEY_CITY = "city";
	private static final String KEY_TEMP = "temp1";
	private static final String KEY_WEATHER = "weather1";
	private static final String KEY_WIND = "wind1";
	
	private WeatherCatchDAO weatherCatchDAO;

	/**
	 * @return the weatherCatchDAO
	 */
	public WeatherCatchDAO getWeatherCatchDAO() {
		return weatherCatchDAO;
	}

	/**
	 * @param weatherCatchDAO the weatherCatchDAO to set
	 */
	public void setWeatherCatchDAO(WeatherCatchDAO weatherCatchDAO) {
		this.weatherCatchDAO = weatherCatchDAO;
	}
	
	public void catchWeather(){
		String infoDate = DateUtil.addDay(0);
		List<Map<String,Object>> wCodeList = weatherCatchDAO.getWeatherCode_City();
		List<Map<String,Object>> wInfoList = new ArrayList<Map<String,Object>>();
		for(Map<String,Object> wCodeMap:wCodeList){
			String netUrl = CATCHWEATHERURL
						+MapGetter.getString(wCodeMap, "WthCC_WeatherCode")
						+CATCHWEATHERURLSUFFIX;
			String wSrcInfo = DownLoadNet.downloadNetSimple(netUrl);
			Map<String,Object> wSrcInfoMap = JsonConvertMap(wSrcInfo);
			Map<String,Object> wInfoMap = new HashMap<String,Object>();
			wInfoMap.put("WthIC_CityName", MapGetter.getString(wSrcInfoMap, KEY_CITY));
			wInfoMap.put("WthIC_SourceInfo", wSrcInfo);
			wInfoMap.put("WthIC_Temp", MapGetter.getString(wSrcInfoMap, KEY_TEMP));
			wInfoMap.put("WthIC_Weather", MapGetter.getString(wSrcInfoMap, KEY_WEATHER));
			wInfoMap.put("WthIC_Wind", MapGetter.getString(wSrcInfoMap, KEY_WIND));
			wInfoList.add(wInfoMap);
		}
		weatherCatchDAO.iWeatherInfo_City(wInfoList, infoDate);
	}
	
	@SuppressWarnings("unchecked")
	public Map<String,Object> JsonConvertMap(String jsonStr){
		ObjectMapper objectMapper= new ObjectMapper();
		Map<String, Object> map1 = null;
		try {
			map1 = objectMapper.readValue(jsonStr,Map.class);
		} catch (JsonParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JsonMappingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(map1==null)return null;
		Map<String,Object> map2 = (Map<String,Object>)map1.get("weatherinfo");
		return map2;
	}
}
